package com.glory.wxelectricitypda.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.QcCheckListAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.QcCheckBean;
import com.glory.wxelectricitypda.bean.QcCheckListBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class QcCheckActivity extends BaseDialogActivity {

    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvNo)
    TextView tvNo;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvWaiXiangHao)
    TextView tvWaiXiangHao;
    @BindView(R.id.tvWaiNum)
    TextView tvWaiNum;
    @BindView(R.id.rvList)
    RecyclerView rvList;
    @BindView(R.id.etHe)
    FreeUI_ClearEditText etHe;
    @BindView(R.id.rbOK)
    RadioButton rbOK;
    @BindView(R.id.rbNG)
    RadioButton rbNG;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rgChoose)
    RadioGroup rgChoose;

    private QcCheckListAdapter qcCheckListAdapter;
    private int choose = 1;
    private String lotId;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qc_check;
    }

    @Override
    protected void initView() {

        rvList.setLayoutManager(new LinearLayoutManager(QcCheckActivity.this));
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.addItemDecoration(new DividerItemDecoration(QcCheckActivity.this, 1));
        qcCheckListAdapter = new QcCheckListAdapter(null);
        qcCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        qcCheckListAdapter.isFirstOnly(true);
        rvList.setAdapter(qcCheckListAdapter);

        freeUI_titleView.setTitle("OQC检验");
        freeUI_titleView.setRightListener("提交检验", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etHe.getText().toString().length()==0){
                    showToast("请扫描盒号",false);
                    return;
                }
                String userName = etUser.getText().toString();
                if (userName.isEmpty()){
                    showToast("请输入检验员",false);
                    return;
                }
                requestUser(userName, new CallBack() {
                    @Override
                    public void success() {
                        requestLotCheck();
                    }

                    @Override
                    public void fail() {
                        showToast("操作人工号验证失败，请重新输入", false);
                    }

                    @Override
                    public void error() {
                        showToast("连接不到服务器，请重试", false);
                    }
                });

            }
        });
    }


    @Override
    protected void initData() {

        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    requestLotData(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etHe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    boolean isHave = false;
                    for (QcCheckBean qcCheckBean : qcCheckListAdapter.getData()) {
                        if (qcCheckBean.getLOTID().equalsIgnoreCase(s.toString())) {
                            isHave = true;
                            break;
                        }
                    }
                    if (!isHave) {
                        showToast("盒号不存在列表中，无法检验", false);
                    } else {
                        boolean isCheck = false;
                        for (QcCheckBean qcCheckBean : qcCheckListAdapter.getData()) {
                            if (!qcCheckBean.getOPERATOR2().isEmpty()) {
                                isCheck = true;
                                break;
                            }
                        }
                        if (isCheck) {
                            showToast("该盒已经检验过,请不要重复检验", false);
                        }else {
                            rbNG.setEnabled(true);
                            rbOK.setEnabled(true);
                            etUser.setEnabled(true);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestLotData(final String lotId) {
        this.lotId = lotId;
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotId);
            jsonObject1.put("ACTIONTYPE", "OQCQUERY");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<QcCheckListBean>>(
                new TypeToken<ResponseBean<QcCheckListBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<QcCheckListBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    etHe.setEnabled(true);
                    etHe.requestFocus();
                    qcCheckListAdapter.setNewData(response.body().getBody().getLOTLIST());
                }
            }

            @Override
            public void onError(Response<ResponseBean<QcCheckListBean>> response) {
                super.onError(response);
                loadError(response.getException(), "QC检验LOT请求");
            }
        });
    }

    private void requestLotCheck() {
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotId);
            jsonObject1.put("ATTRIBUTE1", etHe.getText().toString());
            jsonObject1.put("ACTIONTYPE", "OQC");
            jsonObject1.put("COMMENTS", choose==1?"OK":"NG");
            jsonObject1.put("OPERATOR", etUser.getText().toString());
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<QcCheckListBean>>(
                new TypeToken<ResponseBean<QcCheckListBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<QcCheckListBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                    etLotId.setText("");
                    etLotId.requestFocus();
                    qcCheckListAdapter.setNewData(null);
                    etHe.setEnabled(false);
                    etHe.setText("");
                    rbNG.setEnabled(false);
                    rbOK.setEnabled(false);
                    rbNG.setChecked(false);
                    rbOK.setChecked(true);
                    choose = 1;
                    etUser.setEnabled(false);
                    etUser.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<QcCheckListBean>> response) {
                super.onError(response);
                loadError(response.getException(), "QC检验LOT");
            }
        });
    }


    @OnClick({R.id.rbOK, R.id.rbNG})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbOK:
                choose = 1;
                break;
            case R.id.rbNG:
                choose = 2;
                break;
        }
    }
}
